package com.medicine.common.utils;

import com.medicine.common.exception.WarnException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ExUtils {

    /*public ImprotDTO importFile2(MultipartFile file, String s) throws IOException, WarnException {
        XSSFWorkbook workbook;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
        } catch (Exception e) {
            throw new WarnException("导入模板不正确，请下载正确的模板填写重试");
        }
        //获取第一个sheet
        XSSFSheet sheet = workbook.getSheetAt(0);
        if (sheet.getLastRowNum() <= 1) {
            throw new WarnException("不包含数据");
        }

        String merchantId = UserContext.getUserContext().getMerchantId();
        List<Category> categoryList = categoryService.findByTopId(merchantId);
        Map<String, Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getName, category -> category, (a, b) -> b));
        List<Brand> brandList = brandService.findAll();
        Map<String, String> brandMap = brandList.stream().collect(Collectors.toMap(Brand::getName, Brand::getId, (u1, u2) -> u2));
        ImprotDTO checkData = checkData(sheet, categoryMap, brandMap);
        if (checkData != null) {
            return checkData;
        }

        List<ProductUpdateParam> productList = new ArrayList<>();
        String orgId = UserContext.getUserContext().getOrgId();
        for (int i = 2; i <= sheet.getLastRowNum(); i++) {
            XSSFRow row = sheet.getRow(i);
            if (isEmptyRow(row)) {
                log.info("存在空行，解析结束,进行插入");
                break;
            }
            ProductUpdateParam product = new ProductUpdateParam();
            String typeName = row.getCell(0).toString();
            product.setTypeName(typeName);
            if (ProductTypeCodeEnum.GOODS.getMsg().equals(typeName)) {
                product.setTypeCode(ProductTypeCodeEnum.GOODS.getValue());
            } else if (ProductTypeCodeEnum.DRUG.getMsg().equals(typeName)) {
                product.setTypeCode(ProductTypeCodeEnum.DRUG.getValue());
            } else {
                continue;
            }
            String productBarCode = row.getCell(1) == null ? "" : row.getCell(1).toString();
            product.setProductBarCode(productBarCode);
            product.setName(row.getCell(2).toString());
            Category category = categoryMap.get(row.getCell(3).toString());
            if (category != null) {
                product.setCategoryId(category.getId());
                product.setCategoryName(row.getCell(3).toString());
            }
            if (!isNullRow(row.getCell(4))) {
                String brandId = brandMap.get(row.getCell(4).toString().trim());
                if (brandId != null) {
                    product.setBrandId(brandId);
                    product.setBrandName(row.getCell(4).toString().trim());
                }
            }

            String abroad = row.getCell(5) == null ? "" : row.getCell(5).toString();
            product.setAbroad(abroad);
            String spec = row.getCell(6) == null ? "" : row.getCell(6).toString();
            product.setSpec(spec);
            String unit = row.getCell(7) == null ? "" : row.getCell(7).toString();
            product.setUnit(unit);
            product.setPrice(new BigDecimal(row.getCell(8).toString()));

            Boolean isShop = isNullRow(row.getCell(9)) ? null : getInt(row.getCell(9)) == 1 ? true : false;
            product.setIsShop(isShop);
            Boolean isOnline = isNullRow(row.getCell(10)) ? null : getInt(row.getCell(10)) == 1 ? true : false;
            product.setIsOnline(isOnline);
            productList.add(product);

        }
        workbook.close();
        //批量导入
        if (StringUtils.isEmpty(s)) {
            checkData = checkDistinctData(productList, "");
            if (checkData != null) {
                return checkData;
            }
            List<String> productIdList = new ArrayList<>();
            for (ProductUpdateParam productUpdateParam : productList) {
                productUpdateParam.setCombo(false);
                if (productUpdateParam.getIsShop() == null) {
                    productUpdateParam.setIsShop(true);
                }
                Product product = createProduct(productUpdateParam, orgId);
                productIdList.add(product.getId());
            }
            ImprotDTO dto = new ImprotDTO();
            dto.setCount(sheet.getLastRowNum() - 1);
            dto.setNormalNumber(sheet.getLastRowNum() - 1);
            dto.setSuccessList(productList);
            return dto;
        } else {
            if (productList.size()>200){
                throw new WarnException("批量修改的商品条数每次暂时不能超过200条");
            }
            //批量修改商品价格
            checkData = checkDistinctData(productList, "updatePrice");
            //报错
            if (checkData != null) {
                return checkData;
            }
            //正常返回数据
            List<ProductImportDto> productImportDtos = getUpdateProducts(productList);
            //批量商品修改价格
            updateProduct(productList, orgId);
            ImprotDTO dto = new ImprotDTO();
            dto.setCount(sheet.getLastRowNum() - 1);
            dto.setNormalNumber(sheet.getLastRowNum() - 1);
//            dto.setSuccessList(productList);
            dto.setProductImportDtos(productImportDtos);
            return dto;
        }
    }*/
}
